

cd $directory

clear all
set more off
set obs 1000000
set seed 20210707

local a1 = 0.35
local a2 = 0.53
local a3 = 0.65
local b1 = 0.2
local b2 = 0.63
local b3 = 0.7
local sd = 0.202
foreach map in a b{
forvalues s = 1(1)3{
gen `map'`s' = invlogit(logit(``map'`s'')+ rnormal()*`sd')
gen w_`map'`s' = `map'`s' > 0.5
}

gen pv_`map' = (`map'1 + `map'2 + `map'3)/3
gen close_pv_`map' = abs(pv_`map'-0.5)*3000 

gen normal_`map' = w_`map'1 != w_`map'3
gen all3_`map' = w_`map'1 == w_`map'3 & w_`map'1 == w_`map'2

gen close_ev_`map' = abs(`map'2-0.5)*1000 if normal_`map' == 1 
replace close_ev_`map' = abs(`map'1-0.5)*1000 if normal_`map' != 1&  all3_`map' == 0 & `map'2<`map'1<`map'3
replace close_ev_`map' = abs(`map'3-0.5)*1000 if normal_`map' != 1&  all3_`map' == 0 & `map'1<`map'3<`map'2
replace close_ev_`map' = abs(`map'3-0.5)*1000 if `map'1<`map'3& `map'3<`map'2&w_`map'3!=w_`map'2
replace close_ev_`map' = abs(`map'1-0.5)*1000 if `map'2<`map'1& `map'1<`map'3 & w_`map'1 != w_`map'2
replace close_ev_`map' = abs(`map'3-0.5)*1000 + abs(`map'2-0.5)*1000 if normal_`map' != 1&  all3_`map' == 1 
replace close_ev_`map' = abs(`map'1-0.5)*1000 + abs(`map'2-0.5)*1000 if normal_`map' != 1&  all3_`map' == 1  & abs(`map'1-0.5)*1000 + abs(`map'2-0.5)*1000 < close_ev_`map'
replace close_ev_`map' = abs(`map'1-0.5)*1000 + abs(`map'3-0.5)*1000 if normal_`map' != 1&  all3_`map' == 1  & abs(`map'1-0.5)*1000 + abs(`map'2-0.5)*1000 < close_ev_`map'

gen assumptions_hold_`map' = `map'1<`map'2& `map'2<`map'3&w_`map'3!=w_`map'1&0 < `map'1&`map'3<1
}



sum close_*
sum a1 a2 a3 b1 b2 b3
sum pv_a pv_b
sum assumptions*

foreach v in a1 a2 a3 b1 b2 b3 pv_a pv_b close_pv_a close_ev_a close_pv_b close_ev_b{
	qui sum `v'
	qui local m`v' = r(mean)
}


matrix T=(1000, `ma1', `ma2', `ma3', `mpv_a', `mclose_ev_a', `mclose_pv_a' \ 1000, `mb1', `mb2', `mb3', `mpv_b', `mclose_ev_b', `mclose_pv_b')
matrix list T
xml_tab T, save("tables`c(dirsep)'TableA3") replace
